package HashTab;

import java.awt.*;
import java.util.TreeMap;

/**
 * @author yyq
 * @create 2021-10-25 11:08
 * 哈希表
 * 结构为
 * 指向员工 HeadEmployee{
 *     employee head;
 *     find();
 *     delete();
 *     add();
 *     list();
 *     change();
 * }
 *
 * 员工节点 employee{
 *...
 * }
 *
 * hashTable{
 *     int size;
 *     HeadEmployee[] arr;
 *     find,
 *     add
 *     ...
 *      }
 *
 *
 */
public class Hashtab {
    private Integer size;
    HeadLink[] headLinks;

    public Hashtab(Integer size) {
        this.size=size;
        headLinks=new HeadLink[size];
        for (Integer i = 0; i < size; i++) {
            headLinks[i]=new HeadLink(i);
        }
    }

    public void add(Employee employee){
        int index=employee.id%size;
        headLinks[index].add(employee);
    }

    public void list(){
        for (Integer i = 0; i < size; i++) {
            headLinks[i].list();
        }
    }

    public Employee query(Integer id){
        Integer index=id%size;
        Employee query = headLinks[index].query(id);
        return query;
    }

    public void delete(Integer id){
        Integer index=id%size;
        headLinks[index].delete(id);
    }


}


class Employee{
    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", salary=" + salary +
                '}';
    }

    public Employee next;    //指向下一个节点
    public Integer id;
    public String name;
    public Integer salary;

    public Employee(Integer id, String name, Integer salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }
}

class HeadLink{

    public Employee head=null;
    public Integer sno;

    public HeadLink(Integer sno) {
        this.sno = sno;
    }

    //添加
    //添加后此链表的员工ID有序
    public void add(Employee employee){
        if(head==null){
            head=employee;
            return;
        }
        if(head!=null){
            //如果小于第一个元素ID直接添加
            if(head.id>employee.id){
                employee.next=head;
                head=employee;
            }
            //大于第一个元素ID直接从第二个元素ID比较
            else {
                Employee pre =head;
                Employee last=head.next;
                while (true){
                    if(last==null){
                        pre.next=employee;
                        break;
                    }
                    //大于last元素ID 两个指针后移 继续循环
                    if(employee.id>last.id){
                        pre=last;
                        last=last.next;
                        continue;
                    }
                    //小于last元素ID直接插入
                    if(employee.id<=last.id){
                        employee.next=pre.next;
                        pre.next=employee;
                        break;
                    }
                }
            }
        }

    }
    //全部信息
    public void list(){
        if(head==null){
            System.out.println("第"+sno+"号链表为空!");
            return;
        }
        Employee temp=head;
        System.out.print("第"+sno+"号链表");
        while (true){
            //退出循环条件 当指向空指针时
            if(temp==null) break;
            System.out.print("=> 员工ID"+temp.id+",   ");
            System.out.print("=> 员工姓名"+temp.name+",   ");
            System.out.print("=> 员工薪资"+temp.salary);
            System.out.println();
            temp=temp.next;
        }
    }

    //删除
    public void delete(Integer id){
        Employee pre=head;
        Employee last=head.next;
        //如果正好是第一个 删除掉
        if(pre.id==id){
            head=pre.next;
            return;
        }
        //不是第一个的话
        while(true){
            if(pre.next==null){
                System.out.println("没有查看该员工信息");
                break;
            }
            if(last.id==id){
                pre.next=last.next;
                break;
            }
        }
    }

    //查询
    public Employee query(Integer id){
        Employee temp=head;
        while(true){
            if (temp==null){
                System.out.println("没有找到");
                return null;
            }
            if(temp.id==id) return temp;
            temp=temp.next;
        }
    }
}
